package com.xhwl.centiotdata.suppliersystem.huawei;

import com.alibaba.fastjson.JSONObject;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandRequest;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandResponse;
import com.huaweicloud.sdk.iotda.v5.model.DeviceCommandRequest;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/03/ 10:15
 */
@Service
public class GeneralHuaWeiIoTDAHandler {
    private static final Logger log = LoggerFactory.getLogger(GeneralHuaWeiIoTDAHandler.class);

    public JSONObject command(String ak, String sk, String region, String deviceId, String serviceId, String commandName, Object paras) {
        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf(region))
                .build();
        CreateCommandRequest request = new CreateCommandRequest();
        request.withDeviceId(deviceId);
        DeviceCommandRequest body = new DeviceCommandRequest();
        body.withServiceId(serviceId);
        body.withCommandName(commandName);
        body.withParas(paras);
        request.withBody(body);
        CreateCommandResponse response = null;
        try {
            if(JSONObject.toJSONString(paras).length() < 10240)
            log.info("--授权-【HuaWeiIoTDA command】deviceId:{};serviceId:{};commandName:{};paras:{}", deviceId, serviceId, commandName, JSONObject.toJSONString(paras));
            response = client.createCommand(request);
            log.info("--授权-【HuaWeiIoTDA command】response : {}", JSONObject.toJSONString(response));
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
        }
        JSONObject resultObject = null;
        try {
            if (response == null || response.getHttpStatusCode() != 200) {
                if(JSONObject.toJSONString(paras).length() < 10240)
                log.error("--授权-【HuaWeiIoTDA command request failed】deviceId:{};serviceId:{};commandName:{};paras:{}", deviceId, serviceId, commandName, JSONObject.toJSONString(paras));
                throw new BusinessException();
            }
            resultObject = (JSONObject) JSONObject.toJSON(response.getResponse());
//            if (resultObject.getInteger("Code").intValue() != 200) {
//                log.error("【HuaWeiIoTDA command failed】deviceId:{};serviceId:{};commandName:{};paras:{};Msg:{}", deviceId, serviceId, commandName, JSONObject.toJSONString(paras), resultObject.getString("Msg"));
//                throw new BusinessException();
//            }
        } catch (BusinessException e) {
            if(JSONObject.toJSONString(paras).length() < 10240)
            log.error("--授权-【HuaWeiIoTDA command exception】deviceId:{};serviceId:{};commandName:{};paras:{}", deviceId, serviceId, commandName, JSONObject.toJSONString(paras));
            e.printStackTrace();
            throw new ApplicationException();
        }
        return resultObject;
    }

}
